package personal.xjl._23java2studentdemo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import personal.xjl._23java2studentdemo.mapper.CourseMapper;
import personal.xjl._23java2studentdemo.model.Course;
import personal.xjl._23java2studentdemo.model.Student;
import personal.xjl._23java2studentdemo.service.CourseService;

import java.util.List;

@Service// 标识这是一个Spring服务组件，会被自动扫描并注册为Bean
public class CourseServiceImpl implements CourseService {

    @Autowired// 自动注入课程Mapper
    CourseMapper courseMapper;

    @Override
    public List<Course> findAll() {
        return courseMapper.selectAll();
    }

    @Override
    public int remove(int id) {
        return courseMapper.deleteById(id);
    }

    @Override
    public int add(Course course) {
        return courseMapper.insert(course);
    }

    @Override
    public int edit(Course course) {
        return courseMapper.update(course);
    }

    @Override
    public Course findOneById(int id) {
        return courseMapper.selectById(id);
    }

    @Override
    public  List<Course> findByPage(int pageNo, int pageSize){
        //如果页号<=0，则从第一条记录开始，以防异常
        int fromIndex = (pageNo<1)?0:(pageNo-1)*pageSize;
        return courseMapper.selectByPage(fromIndex, pageSize);
    }
}
